<?php

namespace App\Admin\Controllers;

use App\Models\Gym;
use App\Models\GymTicket;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class GymTicketController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('体验券');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('体验券');
            $content->description('修改');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('体验券');
            $content->description('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(GymTicket::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            $grid->id('ID')->sortable();
            $grid->gym()->name('所属健身房');
            $grid->name('名称');
            $grid->count('总数量');
            $grid->remain_count('剩余数量');
            $grid->pic('图片')->image();
            $grid->status('状态')->display(function ($status) {
                if ($status == 0) {
                    return "<span class=\"label label-primary\">未上线</span>";
                } elseif ($status == 1) {
                    return "<span class=\"label label-success\">上线中</span>";
                } elseif ($status == 2) {
                    return "<span class=\"label label-danger\">已下线</span>";
                }
            });

            $grid->start_time('开始时间')->display(function ($time) {
                return $time ? : '';
            });
            $grid->end_time('结束时间')->display(function ($time) {
                return $time ? : '';
            });
            $grid->created_at('创建时间');

            $grid->disableExport();


            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });

            $grid->filter(function ($filter) {
                $filter->like('gym.name', '所属健身房');
                $filter->like('name', '名称');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(GymTicket::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->hidden('id');
            $form->hidden('gym_id');
            $form->select('gym_id', '所属健身房')->options(function ($id) {
                $gym = Gym::where(['id' => $id, 'is_delete' => 0, 'status' => 1])->first();
                if ($gym) {
                    return [$gym->id => $gym->name];
                }
            })->ajax('/admin/api/gyms')->rules('required', [
                'required' => '所属健身房不能为空'
            ]);
            $form->text('name', '名称')->rules('required', [
                'required' => '名称不能为空'
            ]);
            $form->number('count', '总数量')->default(1);
            $form->number('remain_count', '剩余数量');
            $form->image('pic', '图片')->uniqueName()->rules('required', [
                'required' => '图片不能为空'
            ]);
            $form->select('status', '状态')->options(['0' => '未上线', '1' => '上线中', '2' => '已下线']);
            $form->datetime('start_time', '开始时间')->format('YYYY-MM-DD HH:mm:ss')->rules('required|date');
            $form->datetime('end_time', '结束时间')->format('YYYY-MM-DD HH:mm:ss')->rules('required|date');
            $form->editor('rules', '活动规则')->rules('required', [
                'required' => '活动规则不能为空'
            ]);
            $form->saving(function (Form $form) {
                $where = [['name', '=', $form->name]];
                $id = $form->id;
                $gym_id = $form->gym_id;
                if (!empty($gym_id)) {
                    $where[] = ['gym_id', '=', $gym_id];
                }
                if (!empty($id)) {
                    $where[] = ['id', '<>', $id];
                }
                $is_exist = GymTicket::where($where)->first();
                if (!empty($is_exist)) {
                    $error = new MessageBag([
                        'title'   => '错误',
                        'message' => '广告名称已存在',
                    ]);

                    return back()->with(compact('error'));
                }
                $form->start_time = strtotime($form->start_time);
                $form->end_time = strtotime($form->end_time);
            });
        });
    }
}
